﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;

namespace KeyLock35
{
    public partial class Component1 : Component
    {
        #region Attributs

        private MenuItem[] _menu;
        private ContextMenu _contextMenu;
        private int _nbTouche;

        internal Options _option;
        internal Alerte _alerte;

        internal NotifyIcon _Ntfy_Num = new NotifyIcon();
        internal NotifyIcon _Ntfy_Caps = new NotifyIcon();
        private KeyboardHookListener _keyBoardManager;
        #endregion

        #region Constructeur
        public Component1()
        {
            InitializeComponent();
            this._option = new Options(this);
            this._alerte = new Alerte(this);

            _menu = new MenuItem[] { new MenuItem("Quitter", MenuQuitter),
                                     new MenuItem("Options", MenuOptions)};
            _contextMenu = new ContextMenu(_menu);

            this._Ntfy_Num.Icon = NumLock(false);
            this._Ntfy_Num.Text = "Num Lock";
            this._Ntfy_Num.Visible = true;
            this._Ntfy_Num.BalloonTipTitle = "Num Lock";
            this._Ntfy_Num.MouseDoubleClick += MenuOptions;
            this._Ntfy_Num.BalloonTipClicked += MenuOptions;
            this._Ntfy_Num.ContextMenu = _contextMenu;
            Debug.Print("Ntfy_Num créé");
            
            this._Ntfy_Caps.Icon = CapsLock(false);
            this._Ntfy_Caps.Text = "Caps Lock";
            this._Ntfy_Caps.Visible = true;
            this._Ntfy_Caps.BalloonTipTitle = "Caps Lock";
            this._Ntfy_Caps.ContextMenu = _contextMenu;
            this._Ntfy_Caps.MouseDoubleClick += MenuOptions;
            this._Ntfy_Caps.BalloonTipClicked += MenuOptions;
            Debug.Print("Ntfy_Caps créé");

            this._keyBoardManager = new KeyboardHookListener(new GlobalHooker());
            this._keyBoardManager.Enabled = true;
            this._keyBoardManager.KeyUp += KeysManager;

            this._nbTouche = 0;
        }
        #endregion

        #region Proprietes
        #endregion

        #region Methodes
        public Icon CapsLock(bool baloon)
        {
            bool caps = Console.CapsLock;
            Debug.Print("Caps Lock : {0}", (caps ? "On" : "Off"));
            this._Ntfy_Caps.BalloonTipText = ("Caps Lock : " + (caps ? "On" : "Off"));
            if (baloon && this._option.Ckb_Ballon.Checked)
                this._Ntfy_Caps.ShowBalloonTip(3000);
            if (caps)
                return new Icon(Texte.DecodeSetting(Properties.Settings.Default.IconLock)); //Properties.Resources.Green;
            return new Icon(Texte.DecodeSetting(Properties.Settings.Default.IconUnlock)); //Properties.Resources.Orange;
        }

        public Icon NumLock(bool baloon)
        {
            bool num = Console.NumberLock;
            Debug.Print("Num Lock : {0}", (num ? "On" : "Off"));
            this._Ntfy_Num.BalloonTipText = ("Num Lock : " + (num ? "On" : "Off"));
            if (baloon && this._option.Ckb_Ballon.Checked)
                this._Ntfy_Num.ShowBalloonTip(3000);
            if (num)
                return new Icon(Texte.DecodeSetting(Properties.Settings.Default.IconLock)); //Properties.Resources.Green;
            return new Icon(Texte.DecodeSetting(Properties.Settings.Default.IconUnlock)); //Properties.Resources.Orange;
        }
        #endregion

        #region Controles
        public void MenuQuitter(object sender, System.EventArgs e)
        {
            this._Ntfy_Caps.Dispose();
            this._Ntfy_Num.Dispose();
            Debug.Print("Arret de KeyLock ...");
            System.Environment.Exit(0);
        }

        public void MenuOptions(object sender, System.EventArgs e)
        {
            this._option.Show(false);
        }

        private void KeysManager(object sender, KeyEventArgs e)
        {
            bool Caps = e.KeyCode == Keys.CapsLock;
            bool Num = e.KeyCode == Keys.NumLock;
            bool Caracteres=(e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z);
            if (Caps)
            {
                this._Ntfy_Caps.Icon = CapsLock(true);
            }
            if (Num)
            {
                this._Ntfy_Num.Icon = NumLock(true);
            }
            if ((this._option.Ckb_Alert.Checked|| this._option.Ckb_Ballon.Checked) && Console.CapsLock && Caracteres)
            {
                ++_nbTouche;
                if (_nbTouche >= (int)this._option.Num_NbToucheMaj.Value)
                {
                    this._keyBoardManager.Enabled = false;
                    _nbTouche = 0;
                    Debug.Print("Plus de {0} touches en Maj à la suite.", (int)this._option.Num_NbToucheMaj.Value);
                    this._Ntfy_Caps.BalloonTipText = "Plus de " + this._option.Num_NbToucheMaj.Value + " touches en Maj à la suite.";
                    
                    if (this._option.Ckb_Ballon.Checked)
                        this._Ntfy_Caps.ShowBalloonTip(3000);
                    if (this._option.Ckb_Alert.Checked)
                        this._alerte.Show((int)this._option.Num_NbToucheMaj.Value);

                    this._keyBoardManager.Enabled = true;
                }
            }
            else
                _nbTouche = 0;
        }
        #endregion
    }
}
